<?php
require_once 'modules/dao/baseDao.php';
require_once 'modules/dao/constants.php';

abstract class CommonDao extends BaseDao {
    /**
     * 通用DAO 
     * @author liuhui 2009-6-15
     */
    protected $table_name = null;
    protected $primary_key = null;

    public function __construct () {
        if (empty($this->table_name) || empty($this->primary_key)) {
            throw new Exception('Extends ' . __CLASS__ . ' tablename or primary key cann`t be null.');
        }
    }

    public function add (array $fields) {
        if (count($fields) < 1) {
            return null;
        }
        try {
            $result = $this->_insert($fields, $this->table_name);
        } catch (Exception $e) {
            (Consts::EXCEP) ? $this->getException($e) : Consts::D_EXCEP;
        }
        return $result;
    }

    public function get ($primary_key) {
        $primary_key = $this->checkInt($primary_key);
        if ($primary_key < 1) {
            return NULL;
        }
        $wheres = array($this->primary_key => $primary_key);
        try {
            $result = $this->_get($this->table_name, $wheres, $this->_buildLimitSQL());
        } catch (Exception $e) {
            (Consts::EXCEP) ? $this->getException($e) : Consts::D_EXCEP;
        }
        return $result;
    }

    public function update ($fields, $primary_key) {
        $primary_key = $this->checkInt($primary_key);
        if ($primary_key < 1 || count($fields) < 1) {
            return NULL;
        }
        $wheres = array($this->primary_key => $primary_key);
        try {
            $result = $this->_update($fields, $this->table_name, $wheres, $this->_buildLimitSQL());
        } catch (Exception $e) {
            (Consts::EXCEP) ? $this->getException($e) : Consts::D_EXCEP;
        }
        return $result;
    }

    public function delete ($primary_key) {
        $primary_key = $this->checkInt($primary_key);
        if ($primary_key < 1) {
            return NULL;
        }
        $wheres = array($this->primary_key => $primary_key);
        try {
            $result = $this->_delete($this->table_name, $wheres, $this->_buildLimitSQL());
        } catch (Exception $e) {
            (Consts::EXCEP) ? $this->getException($e) : Consts::D_EXCEP;
        }
        return $result;
    }

    /**
     * @desc 二级取列表函数 这里所说的二级是与继承上一级相关
     *
     * @param string $wheres
     * @param int $offset
     * @param int $limit
     * @return array()
     */
    public function __gets ($wheres = '', $offset = 1, $limit = 0) {
        try {
            $result = $this->_gets($this->table_name, $wheres, $this->_buildLimitSQL($offset, $limit));
        } catch (Exception $e) {
            (Consts::EXCEP) ? $this->getException($e) : Consts::D_EXCEP;
        }
        return $result;
    }

    /**
     * @desc 二级取统计函数 这里所说的二级是与继承上一级相关
     *
     * @param string $wheres
     * @return int
     */
    public function __count ($wheres = '') {
        try {
            $result = $this->_count($this->table_name, $wheres, $this->_buildLimitSQL());
        } catch (Exception $e) {
            (Consts::EXCEP) ? $this->getException($e) : Consts::D_EXCEP;
        }
        return $result;
    }
}
